package com.example.yls.broadcastreceiver_sendorder;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
private LowPriorityReceiver lowPriorityReceiver;
 private MiddlePriorityReceiver middlePriorityReceiver;
 private HighReceiver highReceiver;
 private IntentFilter intentFilter;
 private NetworkChangeReceiver networkChangeReceiver;
 private Button button;


/*监听网络变化*/
    public class NetworkChangeReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent){
            Toast.makeText(context, "network changes", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /*在 onCreate() 方法中注册*/
        intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        networkChangeReceiver = new NetworkChangeReceiver();
        registerReceiver(networkChangeReceiver, intentFilter);

/* 有序广播*/
        lowPriorityReceiver = new LowPriorityReceiver();
        IntentFilter lowFilter = new IntentFilter();
        lowFilter.addAction("ORDERED_BROADCASDT");
        lowFilter.setPriority(0);
        registerReceiver(lowPriorityReceiver,lowFilter);


        middlePriorityReceiver = new MiddlePriorityReceiver();
        IntentFilter middleFilter = new IntentFilter();
        middleFilter.addAction("ORDERED_BROADCASDT");
        middleFilter.setPriority(50);
        registerReceiver(middlePriorityReceiver,middleFilter);

        highReceiver = new HighReceiver();
        IntentFilter highFilter = new IntentFilter();
        highFilter.addAction("ORDERED_BROADCASDT");
        highFilter.setPriority(100);
        registerReceiver(highReceiver,highFilter);



        findViewById(R.id.btn_1).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction("ORDERED_BROADCASDT");
                sendOrderedBroadcast(intent,null);


            }
        });

        /*并在 onCreate() 方法中添加点击事件*/
        Button button = (Button)findViewById(R.id.btn_2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
                sendBroadcast(intent);
            }
        });
    }
   /* 取消注册*/
    protected void onDestroy(){
        super.onDestroy();
        unregisterReceiver(networkChangeReceiver);
    }


   /* 重启机器或模拟器就可以收到开机广播*/
    public static class MyBroadcastReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent){
            Toast.makeText(context, "received in MyBroadcastReceiver", Toast.LENGTH_SHORT).show();
        }
    }
}
